package com.ship.boats.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ship.boats.business.impl.BoatsSystemServiceImpl;
import com.ship.boats.domain.SpBoatsEventMessage;
import com.ship.boats.domain.vo.QueryBoatsInfo;
import com.ship.boats.domain.vo.QueryBoatsMessageInfo;
import com.ship.boats.mapper.BoatsEventMessageMapper;
import com.ship.boats.service.BoatsEventMessageService;
import com.ship.common.core.constant.ShipApiConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <b>BoatsEventMessageServiceImpl</b>
 *
 * @description: BoatsEventMessageServiceImpl <br>
 * @date: 2024/6/25 15:38 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class BoatsEventMessageServiceImpl extends ServiceImpl<BoatsEventMessageMapper, SpBoatsEventMessage> implements BoatsEventMessageService {
    private static final Logger logger = LoggerFactory.getLogger(BoatsEventMessageServiceImpl.class);

    @Autowired
    private BoatsSystemServiceImpl boatsSystemServiceImpl;

    @Override
    public void getBoatsEventMessage() {
        //查询所有船舶mmsi信息
        List<QueryBoatsMessageInfo> boatsMessageList = baseMapper.queryShipBaseAll();
        //查询船舶信息imo号
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.HOUR_OF_DAY,-1);
//        calendar.add(Calendar.DAY_OF_MONTH,-1000);
        Date time = calendar.getTime();
        long startTime = time.getTime();
        long endTime = new Date().getTime();
        String startDate = String.valueOf(startTime / 1000);
        String endDate = String.valueOf(endTime / 1000);
        if (CollectionUtil.isNotEmpty(boatsMessageList)){
            boatsMessageList.forEach(item -> {
                Map<String,String> map = new HashMap<>();
                map.put("mmsiList",item.getMmsi());
                Object boatsUtil = boatsSystemServiceImpl.getBoatsUtil(map, ShipApiConstants.QUERY_BOATS_ID_URL);
                List<QueryBoatsInfo> boatsList = JSONUtil.toList(JSONUtil.parseArray(boatsUtil), QueryBoatsInfo.class);
                if (CollectionUtil.isNotEmpty(boatsList)){
                    Map<String,String> eventMap = new HashMap<>();
                    eventMap.put("shipId",boatsList.get(0).getShipId());
                    eventMap.put("startTime",startDate);
                    eventMap.put("endTime",endDate);
                    Object resultData = boatsSystemServiceImpl.getBoatsUtil(eventMap, ShipApiConstants.EVENT_MESSAGE_URL);
                    logger.info("获取事件信息返回结果:{}"+JSONUtil.parse(resultData));
                    if (null != resultData){
                        List<SpBoatsEventMessage> eventMessagesList = JSONUtil.toList(JSONUtil.parseArray(resultData), SpBoatsEventMessage.class);
                        eventMessagesList.forEach(value -> {
                            value.setShipName(item.getShipName());
                            value.setImo(item.getImo());
                            Date date = new Date(Long.valueOf(value.getPosTime())*1000);
                            value.setPosTime(DateUtil.format(date,DatePattern.NORM_DATETIME_FORMATTER));
                            value.setCreateTime(new Date());
                            value.setUpdateTime(new Date());
                            baseMapper.insert(value);
                        });
                    }
                }
            });
        }
    }

    public static void main(String[] args) {
        Date startDate = DateUtil.parse("2024-06-01 00:00:00");
        Date endDate = DateUtil.parse("2024-07-12 00:00:00");
        System.out.println(startDate.getTime()/1000);
        System.out.println(endDate.getTime()/1000);
    }
}
